package xyz.yuzegod.megawalls.classes.normal.dreadlord;

import org.bukkit.Sound;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.WitherSkull;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.classes.Classes;
import xyz.yuzegod.megawalls.classes.Skill;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.stats.KitStatsContainer;
import xyz.yuzegod.megawalls.util.ParticleEffect;
import xyz.yuzegod.megawalls.util.PlayerUtils;

import java.util.ArrayList;
import java.util.List;

public class MainSkill extends Skill
{
    public MainSkill(final Classes classes) {
        super("影爆", classes);
    }
    
    @Override
    public int maxedLevel() {
        return 5;
    }
    
    @Override
    public double getAttribute(final int level) {
        switch (level) {
            case 1: {
                return 5.0;
            }
            case 2: {
                return 5.5;
            }
            case 3: {
                return 6.0;
            }
            case 4: {
                return 6.5;
            }
            case 5: {
                return 7.0;
            }
            default: {
                return 5.0;
            }
        }
    }
    
    @Override
    public List<String> getInfo(final int level) {
        final List<String> lore = new ArrayList<String>();
        if (level == 1) {
            lore.add(" §8▪ §7一次发射三个凋零骷髅,");
            lore.add("   §7每个造成§a" + this.getAttribute(level) + "§7点伤害。");
            return lore;
        }
        lore.add(" §8▪ §7一次发射三个凋零骷髅,");
        lore.add("   §7每个造成§8" + this.getAttribute(level - 1) + " ➜ §a" + this.getAttribute(level) + "§7点伤害。");
        return lore;
    }
    
    @Override
    public void upgrade(final GamePlayer gamePlayer) {
        final KitStatsContainer kitStats = gamePlayer.getPlayerStats().getKitStats(this.getClasses());
        kitStats.addSkillLevel();
    }
    
    @Override
    public int getPlayerLevel(final GamePlayer gamePlayer) {
        return gamePlayer.getPlayerStats().getKitStats(this.getClasses()).getSkillLevel();
    }
    
    @Override
    public boolean use(final GamePlayer gamePlayer, final KitStatsContainer kitStats) {
        final Player player = gamePlayer.getPlayer();
        for (int i = 1; i <= 3; ++i) {
            final WitherSkull witherSkull = (WitherSkull)player.launchProjectile((Class)WitherSkull.class);
            witherSkull.setMetadata(MegaWalls.getMetadataValue(), (MetadataValue)new FixedMetadataValue((Plugin)MegaWalls.getInstance(), (Object)gamePlayer.getGameTeam()));
            witherSkull.setVelocity(player.getLocation().add(0.0, 0.0, i * 2.0).getDirection());
            new BukkitRunnable() {
                public void run() {
                    if (witherSkull.isDead() || witherSkull.isOnGround()) {
                        ParticleEffect.EXPLOSION_HUGE.display(0.0f, 0.0f, 0.0f, 1.0f, 1, witherSkull.getLocation(), 30.0);
                        witherSkull.getWorld().playSound(witherSkull.getLocation(), Sound.EXPLODE, 1.0f, 0.0f);
                        final int level = kitStats.getSkillLevel();
                        for (final Player player1 : PlayerUtils.getNearbyPlayers((Entity)witherSkull, 10.0)) {
                            final GamePlayer gamePlayer1 = GamePlayer.get(player1.getUniqueId());
                            if (!gamePlayer1.isSpectator()) {
                                if (gamePlayer.getGameTeam().isInTeam(gamePlayer1)) {
                                    continue;
                                }
                                player1.damage(MainSkill.this.getAttribute(level));
                            }
                        }
                        if (!witherSkull.isDead()) {
                            witherSkull.remove();
                        }
                        this.cancel();
                        return;
                    }
                    ParticleEffect.LAVA.display(0.0f, 0.0f, 0.0f, 1.0f, 5, witherSkull.getLocation(), 30.0);
                }
            }.runTaskTimer((Plugin)MegaWalls.getInstance(), 0L, 1L);
        }
        return true;
    }
}
